﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace BaiDuSitemap.Tools
{
    /// <summary>
    /// 数据导出类
    /// </summary>
    public class Export
    {
        #region ToCSV
        
        /// <summary>
        /// 将数据导出为CSV
        /// </summary>
        /// <param name="listView">数据源：ListView</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2CSV(ListView listView, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    RecordLine += "\"" + listView.Columns[i].Text + "\",";
                }
                text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                text.Flush();

                for (int i = 0; i < listView.Items.Count; i++)
                {
                    RecordLine = "";
                    for (int j = 0; j < listView.Columns.Count; j++)
                    {
                        RecordLine += "\"" + listView.Items[i].SubItems[j].Text + "\",";
                    }
                    text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为CSV
        /// </summary>
        /// <param name="dgView">数据源：DataGridView</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2CSV(DataGridView dgView, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < dgView.ColumnCount; i++)
                {
                    RecordLine += "\"" + dgView.Columns[i].HeaderText + "\",";
                }
                text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                text.Flush();

                for (int i = 0; i < dgView.RowCount; i++)
                {
                    RecordLine = "";
                    for (int j = 0; j < dgView.ColumnCount; j++)
                    {
                        RecordLine += "\"" + dgView.Rows[i].Cells[j].Value + "\",";
                    }
                    text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为CSV
        /// </summary>
        /// <param name="listView">数据列头源：ListView</param>
        /// <param name="SelectedItems">数据源：SelectedListViewItemCollection</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2CSV(ListView listView, ListView.SelectedListViewItemCollection SelectedItems, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    RecordLine += "\"" + listView.Columns[i].Text + "\",";
                }
                text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                text.Flush();

                foreach (ListViewItem Item in SelectedItems)
                {
                    RecordLine = "";
                    for (int j = 0; j < listView.Columns.Count; j++)
                    {
                        RecordLine += "\"" + Item.SubItems[j].Text + "\",";
                    }
                    text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为CSV
        /// </summary>
        /// <param name="cols">数据源列头：List&lt;string&gt;</param>
        /// <param name="vals">数据源：List&lt;string&gt;</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2CSV(List<string> cols, List<string> vals, string strFilePath)
        {
            bool isfile = false;
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                if (File.Exists(strFilePath)) isfile = true;
                fs = new FileStream(strFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                if (!isfile)
                {
                    foreach (string col in cols)
                    {
                        RecordLine += "\"" + col + "\",";
                    }
                    text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                    text.Flush();
                }

                RecordLine = "";
                foreach (string val in vals)
                {
                    RecordLine += "\"" + val + "\",";
                }
                text.WriteLine(RecordLine.Remove(RecordLine.Length - 1));
                text.Flush();
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        #endregion

        #region ToTxt

        /// <summary>
        /// 将数据导出为TXT
        /// </summary>
        /// <param name="listView">数据源：ListView</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2Txt(ListView listView, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    RecordLine += listView.Columns[i].Text + "\t";
                }
                text.WriteLine(RecordLine);
                text.Flush();

                for (int i = 0; i < listView.Items.Count; i++)
                {
                    RecordLine = "";
                    for (int j = 0; j < listView.Columns.Count; j++)
                    {
                        RecordLine += listView.Items[i].SubItems[j].Text + "\t";
                    }
                    text.WriteLine(RecordLine);
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Close();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为TXT
        /// </summary>
        /// <param name="dgView">数据源：DataGridView</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2Txt(DataGridView dgView, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < dgView.ColumnCount; i++)
                {
                    RecordLine += dgView.Columns[i].HeaderText + "\t";
                }
                text.WriteLine(RecordLine);
                text.Flush();

                for (int i = 0; i < dgView.RowCount; i++)
                {
                    RecordLine = "";
                    for (int j = 0; j < dgView.ColumnCount; j++)
                    {
                        RecordLine += dgView.Rows[i].Cells[j].Value + "\t";
                    }
                    text.WriteLine(RecordLine);
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Close();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为TXT
        /// </summary>
        /// <param name="listView">数据列头源：ListView</param>
        /// <param name="SelectedItems">数据源：SelectedListViewItemCollection</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2Txt(ListView listView, ListView.SelectedListViewItemCollection SelectedItems, string strFilePath)
        {
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                fs = new FileStream(strFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    RecordLine += listView.Columns[i].Text + "\t";
                }
                text.WriteLine(RecordLine);
                text.Flush();

                foreach (ListViewItem Item in SelectedItems)
                {
                    RecordLine = "";
                    for (int j = 0; j < listView.Columns.Count; j++)
                    {
                        RecordLine += Item.SubItems[j].Text + "\t";
                    }
                    text.WriteLine(RecordLine);
                    text.Flush();
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        /// <summary>
        /// 将数据导出为TXT
        /// </summary>
        /// <param name="cols">数据源列头：List&lt;string&gt;</param>
        /// <param name="vals">数据源：List&lt;string&gt;</param>
        /// <param name="strFilePath">目标文件的路径</param>
        /// <returns>bool</returns>
        public static bool Export2Txt(List<string> cols, List<string> vals, string strFilePath)
        {
            bool isfile = false;
            FileStream fs = null;
            StreamWriter text = null;
            string RecordLine = "";

            try
            {
                if (File.Exists(strFilePath)) isfile = true;
                fs = new FileStream(strFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
                text = new StreamWriter(fs, Encoding.Default);
                if (!isfile)
                {
                    foreach (string col in cols)
                    {
                        RecordLine += col + "\t";
                    }
                    text.WriteLine(RecordLine);
                    text.Flush();
                }

                RecordLine = "";
                foreach (string val in vals)
                {
                    RecordLine += val + "\t";
                }
                text.WriteLine(RecordLine);
                text.Flush();
            }
            catch
            {
                return false;
            }
            finally
            {
                fs.Dispose();
            }
            return true;
        }

        #endregion
    }
}
